﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;

using System.Text;
using System.Windows.Forms;
using Hopthoai;
using System.IO;
using QUANLYCHAMCONG.BL.BUS;
using QUANLYCHAMCONG.BL.Entities;

namespace QUANLYCHAMCONG.PL.CMS.Maychamcong
{
    public partial class Docdulieutufile : UserControl
    {
        int flag = 0;
        int Num = 0;
        public delegate void _Close();
        public event _Close Close;
        public Total UcStatusRoom { get; set; }
        frmHoidap f;
        CHAMCONGCollection dschamcongtufile;
        CHAMCONG cc;
        public Docdulieutufile()
        {
            InitializeComponent();
        }
        public void ReloadStattus()
        { 
            Thongke tk =new Thongke();
            this.UcStatusRoom.DtData = BAOCAOBUS.ThongkeTotal(tk);
            this.UcStatusRoom.Settext();
        }
        private void SetLink()
        {
            this.links1.SetValues("Máy", "Thêm chấm công  từ danh sách");
        }
        private void Docdulieutufile_Load(object sender, EventArgs e)
        {
            SetLink();
            this.toolbar1.SetToolBar(true, false, false, false, true, false, false, false);
        }
        private void toolbar1_Refresh()
        {
            this.toolbar1.SetToolBar(true, false, false, false, true, false, false, false);
            txtlink.Enabled = false;
            btnmo.Enabled = false;
            btndoc.Enabled = false;
        }
        private void btnmo_Click(object sender, EventArgs e)
        {
            if (flag == 1)
            {
                try
                {
                    OpenFileDialog open = new OpenFileDialog();
                    open.Filter = "Excel File(*.XLS)|*.XLS|All files (*.*)|*.*";
                    open.Multiselect = false;
                    open.ShowDialog();
                    if (!String.IsNullOrEmpty(open.FileName))
                    {
                        string tenfile = open.SafeFileName;
                        string filename = open.FileName;
                        string current = Application.StartupPath;
                        string chuoi = current + "\\App_Data\\" + tenfile;
                        if (!Directory.Exists(current+"\\App_Data\\"))
                        {
                            Directory.CreateDirectory(current + "\\App_Data\\");
                        }
                        if (File.Exists(chuoi))
                        {
                            txtlink.Text = chuoi;
                        }
                        else
                        {
                            File.Copy(filename, chuoi);
                            txtlink.Text = chuoi;
                        }
                    }
                }
                catch { return; }
            }
        }
        private void toolbar1_Add()
        {
            this.flag = 1;
            this.toolbar1.SetToolBar(false, false, false, true, true, false, true, false);
            txtlink.Enabled = true;
            CheckAvaibleFileData();
        }
        void CheckAvaibleFileData()
        {
            string FileName = "ghinhan.xls";
            FileName.ToUpper();
            string chuoi = Application.StartupPath +"\\App_Data\\" + FileName;
            if (File.Exists(chuoi))
            {
                txtlink.Text = chuoi;
                ReadExcel();
            } 
            else
            {
                f = new frmHoidap("Không có ghi nhận.Vui lòng kiểm tra!", frmHoidap.CO.an, frmHoidap.KHONG.an, frmHoidap.THOAT.an, frmHoidap.HINH.baoloi);
                f.ShowDialog();
                btnmo.Enabled = true;
                btndoc.Enabled = true;
            }

        }
        void ReadExcel()
        {
            try
            {
                DataTable dt = new DataTable();
                if (!String.IsNullOrEmpty(txtlink.Text))
                {
                    dt = QUANLYCHAMCONG.BL.BUS.Common.GetExcelTable(txtlink.Text);
                    Num = dt.Rows.Count;
                    dschamcong.DataSource = dt;
                }
            }
            catch (Exception ex)
            {
                f = new frmHoidap("Có lỗi khi mở file " + ex.Message, frmHoidap.CO.an, frmHoidap.KHONG.an, frmHoidap.THOAT.an, frmHoidap.HINH.baoloi);
                f.ShowDialog();
            }
        }
        private void btndoc_Click(object sender, EventArgs e)
        {
            ReadExcel();
        }
        private void toolbar1_Close()
        {
            if (this.Close != null)
                this.Close();
        }
        private void toolbar1_Save()
        {
            if (this.flag==1)//tuc la them moi
            {
                dschamcongtufile = new CHAMCONGCollection();
                if (dschamcong.Rows.Count!=0)
                {
                    try
                    {
                        foreach (DataGridViewRow row in dschamcong.Rows)
                        {
                            cc = new CHAMCONG();
                            cc.IDMAY = int.Parse(row.Cells[5].Value.ToString());
                            cc.MANV = null;
                            cc.NGAY = Convert.ToDateTime(row.Cells[4].Value.ToString()).Date;
                            cc.NGAYGIO = Convert.ToDateTime(row.Cells[4].Value.ToString());
                            cc.MACCM = row.Cells[0].Value.ToString();
                            cc.LOAI = "I";
                            cc.NGUON = "PC";
                            dschamcongtufile.Add(cc);
                        }
                        //THUC HIEN INSERT CHAM CONG 
                        bool ISCOMPLTE = true;
                        int count = 0;
                        foreach (CHAMCONG chamcong in dschamcongtufile)
                        {
                            ISCOMPLTE = CHAMCONGBUS.Insert(chamcong, 0);
                            count++;
                        }
                        if (ISCOMPLTE)
                        {
                            f = new frmHoidap("Cập nhật chấm công thành công!", frmHoidap.CO.an, frmHoidap.KHONG.an, frmHoidap.THOAT.an, frmHoidap.HINH.thongtin);
                            f.ShowDialog();
                        } 
                        else
                        {

                           int dem =0;
                           foreach(CHAMCONG chamhong in dschamcongtufile)
                           {
                               CHAMCONGBUS.Delete(chamhong);
                               if (dem == count)
                                   break;
                               dem++;
                           }
                           f = new frmHoidap("Có lỗi khi thêm chấm công từ File", frmHoidap.CO.an, frmHoidap.KHONG.an, frmHoidap.THOAT.an, frmHoidap.HINH.baoloi);
                           f.ShowDialog();
                        }
                    }
                    catch (System.Exception ex)
                    {
                        f = new frmHoidap("Có lỗi " + ex.Message.ToString(), frmHoidap.CO.an, frmHoidap.KHONG.an, frmHoidap.THOAT.an, frmHoidap.HINH.baoloi);
                        f.ShowDialog();
                        btndoc.Enabled = true;
                        btnmo.Enabled = true;
                    }
                    finally
                    {
                        this.toolbar1.SetToolBar(true, false, false, false, true, false, false, false);
                        txtlink.Enabled = false;
                        btnmo.Enabled = false;
                        btndoc.Enabled = false;
                        ReloadStattus();
                    }
                    
                } 
                else
                {
                    f = new frmHoidap("Không có dữ liệu", frmHoidap.CO.an, frmHoidap.KHONG.an, frmHoidap.THOAT.an, frmHoidap.HINH.baoloi);
                    f.ShowDialog();
                    this.toolbar1.SetToolBar(true, false, false, false, true, false, false, false);
                    txtlink.Enabled = false;
                    btnmo.Enabled = false;
                    btndoc.Enabled = false;
                }

               
            }

        }

    }
}
